/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Netz;

/**
 * Hier kann sich der Spieler mit dem Server verbinden und sich anmelden.
 * @author Daniel Mueller, Phillip Hertel, Jonas Hauff
 */
public class ClientStart extends javax.swing.JFrame {
    private NetzeClient client;
    private Lobbys lobby;
    /**
     * Creates new form ClientStart
     */
    public ClientStart() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Verbinden = new javax.swing.JButton();
        Port = new javax.swing.JTextField();
        Namet = new javax.swing.JTextField();
        Name = new javax.swing.JLabel();
        IpLabel = new javax.swing.JLabel();
        PortLabel = new javax.swing.JLabel();
        Ip = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Verbinden.setText("Verbinden");
        Verbinden.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VerbindenActionPerformed(evt);
            }
        });

        Port.setText("2525");

        Namet.setText("Toast");
        Namet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NametActionPerformed(evt);
            }
        });

        Name.setText("Name");

        IpLabel.setText("Server Ip");

        PortLabel.setText("Server Port");

        Ip.setText("localhost");

        jTextField1.setText("Client 2000");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PortLabel)
                            .addComponent(Name)
                            .addComponent(IpLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Ip, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(Port)
                            .addComponent(Namet)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Verbinden, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(242, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(IpLabel)
                    .addComponent(Ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PortLabel)
                    .addComponent(Port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Name)
                    .addComponent(Namet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Verbinden)
                .addGap(0, 161, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**
 * Wenn der Button zum "Verbinden" gedrückt wird, wird eine Verbindung zwischen Client 
 * und Server aufgebaut und der Spieler unter dem eingegebenem Namen angemeldet und in die Lobby weitergeleitet.
 * @param evt 
 */
    private void VerbindenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VerbindenActionPerformed
        client = new NetzeClient(Ip.getText(),Integer.parseInt(Port.getText()),this.Namet.getText());
        client.pSend("Login: "+this.Namet.getText());
        
        if(client.istVerbunden())
        {
            lobby = new Lobbys();
            lobby.setClient(client);
            lobby.setVorgaenger(this);
            lobby.setpName(Namet.getText());
            lobby.setVisible(true);
            this.setVisible(false);
            client.setLobby(lobby);
        }
    }//GEN-LAST:event_VerbindenActionPerformed

    private void NametActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NametActionPerformed
        
    }//GEN-LAST:event_NametActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientStart().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Ip;
    private javax.swing.JLabel IpLabel;
    private javax.swing.JLabel Name;
    private javax.swing.JTextField Namet;
    private javax.swing.JTextField Port;
    private javax.swing.JLabel PortLabel;
    private javax.swing.JButton Verbinden;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
